<template>
  <div class="user-info-card">
    <el-card class="user-card">
      <div class="user-header">
        <el-avatar :src="props.profile.avatar" size="large" class="avatar" />
        <div class="user-details">
          <h3>{{ props.profile.name }}</h3>
          <p>手机号：{{ props.profile.phone }}</p>
        </div>
      </div>
      <div class="user-footer">
        <el-button
          :color="colorManager.getColor('__secondary_color')"
          type="primary"
          @click="editUserInfo"
          >编辑资料</el-button
        >
      </div>
    </el-card>
  </div>
  <submit-dialog
    v-model:dialogVisibleModel="isDialogShow"
    dialogTitle="编辑个人信息"
    @submit="handleSaveOrUpdateSubmit"
    @reset="resetForm"
  >
    <template #rows>
      <!-- 表单 -->
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="formData"
        status-icon
        label-width="auto"
        class=""
      >
        <!-- 代码生成区域 -->
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="formData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <upload-image-box
            v-model:imageUrl="formData.avatar"
          ></upload-image-box>
        </el-form-item>
      </el-form>
    </template>
  </submit-dialog>
</template>

<script setup>
import { ref } from "vue";
import { updateUser } from "@/api/auth/user/user.js";
import { ElMessageBox } from "element-plus";
import { checkResult } from "@/utils/Rutil.js";
import { useUserStore } from "@/store/auth/userStore.js";
import { colorManager } from "@/assets/scss/colors-util.js";

const userStore = useUserStore();
// el-dialog实体
const ruleFormRef = ref();

const props = defineProps({
  profile: {
    default: {
      avatar: "",
      name: "暂无信息", // 用户名
      phone: "错误", // 电话
    },
  },
  isSelf: {
    default: true,
  },
});
console.log("profile", props.profile);

// 表单数据
const formData = ref({});
// 拷贝数据
formData.value = Object.assign({}, props.profile);
// 是否显示
const isDialogShow = ref(false);

// 表单提交handle
const handleSaveOrUpdateSubmit = async () => {
  console.log("formData", formData.value);
  var codeCheck = false;
  const res = await updateUser(formData.value);
  console.log("handleSaveOrUpdateSubmit-updateUser-res", res);
  codeCheck = checkResult(res);
  if (codeCheck) {
    isDialogShow.value = false;
    ElMessageBox({ message: "提交成功", type: "success" });
  } else {
    ElMessageBox({ message: "提交失败", type: "error" });
  }
  if (props.isSelf) {
    // 主动更新user的profile
    userStore.updateProfile();
  }
};

// 重置表单数据
const resetForm = () => {
  // 调用组件对象的时候，也需要使用value
  ruleFormRef.value.resetFields();
};

function editUserInfo() {
  isDialogShow.value = true;
}
</script>

<style scoped>
.user-info-card {
  padding: 10px;
}

.user-card {
  width: 400px;
  border-radius: 8px;
  padding: 20px;
}

.user-header {
  display: flex;
  align-items: center;
}

.avatar {
  height: 100px;
  width: 100px;
  margin-right: 20px;
}

.user-details h3 {
  margin: 0;
  font-size: 18px;
}

.user-details p {
  margin: 5px 0;
  color: #888;
}

.user-footer {
  margin-top: 20px;
  text-align: center;
}

.el-button {
  width: 100%;
}
</style>
